import shlex
import re
lexer = shlex.shlex(open("test", "r"))
lexer.wordchars = lexer.wordchars + "._A-Za-z0-9"
Identifier = re.compile("[a-zA-Z][a-zA-Z0-9_]*")

tabc=-1
		
def readIdent(lexer):
	token = lexer.get_token()
	if Identifier.match(token):
		return token
	else: 
		lexer.push_token(token)
		return None

def readDecl(lexer):
	type = readIdent(lexer)
	
	if type == "struct":
		parseStructDefinition(lexer)
	decl = readIdent(lexer)
	semi = lexer.get_token()
	#print type,decl, semi
	
	if semi == ";":
		if type and decl:
			return [type,decl]
	else:
		lexer.push_token(semi)
		return None
	
def parseTypedefinition(lexer):
	token = lexer.get_token()
	if token  == "struct":
		parseStructDefinition(lexer)

def parseStructDefinition(lexer):
	# read tag name 
	global tabc
	tabc = tabc+1
	tagName=readIdent(lexer)
	if tagName:
		print tabc*'\t'
		print "struct start:", tagName
	else:
		pass
		#print "struct start(anon):"
	if lexer.get_token() == "{":
		while 1:
			dec = readDecl(lexer)
			if dec is None:
				break
			else:
				print dec
	else:
		print 'Declaration'
	if lexer.get_token() == "}":
		varName=readIdent(lexer)
		print varName
		print "end"
	 
while 1:
	token = lexer.get_token()
	if not token:
		break
	if token == "struct":
		parseStructDefinition(lexer)
	elif token == "typedef":
		parseTypedefinition(lexer)

